using System;
using System.Security.Cryptography;

namespace Wantn.Share.DataCry
{
	public sealed class RSA
	{
		public static bool GenKey(out byte[] pubKey, out byte[] priKey)
		{
			bool flag = false;
			RSACryptoServiceProvider rSACryptoServiceProvider = null;
			try
			{
				rSACryptoServiceProvider = new RSACryptoServiceProvider();
				pubKey = rSACryptoServiceProvider.ExportCspBlob(includePrivateParameters: false);
				priKey = rSACryptoServiceProvider.ExportCspBlob(includePrivateParameters: true);
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				rSACryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Encrypt(byte[] data, byte[] pubKey)
		{
			byte[] array = null;
			RSACryptoServiceProvider rSACryptoServiceProvider = null;
			try
			{
				rSACryptoServiceProvider = new RSACryptoServiceProvider();
				rSACryptoServiceProvider.ImportCspBlob(pubKey);
				return rSACryptoServiceProvider.Encrypt(data, fOAEP: true);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				rSACryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Encrypt(byte[] data, out byte[] pubKey, out byte[] priKey)
		{
			byte[] array = null;
			RSACryptoServiceProvider rSACryptoServiceProvider = null;
			try
			{
				rSACryptoServiceProvider = new RSACryptoServiceProvider();
				pubKey = rSACryptoServiceProvider.ExportCspBlob(includePrivateParameters: false);
				priKey = rSACryptoServiceProvider.ExportCspBlob(includePrivateParameters: true);
				return rSACryptoServiceProvider.Encrypt(data, fOAEP: true);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				rSACryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Decrypt(byte[] data, byte[] priKey)
		{
			byte[] array = null;
			RSACryptoServiceProvider rSACryptoServiceProvider = null;
			try
			{
				rSACryptoServiceProvider = new RSACryptoServiceProvider();
				rSACryptoServiceProvider.ImportCspBlob(priKey);
				return rSACryptoServiceProvider.Decrypt(data, fOAEP: true);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				rSACryptoServiceProvider?.Clear();
			}
		}

		public static byte[] Sign(byte[] data, byte[] priKey)
		{
			byte[] array = null;
			RSACryptoServiceProvider rSACryptoServiceProvider = null;
			try
			{
				rSACryptoServiceProvider = new RSACryptoServiceProvider();
				rSACryptoServiceProvider.ImportCspBlob(priKey);
				return rSACryptoServiceProvider.SignData(data, new SHA1CryptoServiceProvider());
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				rSACryptoServiceProvider?.Clear();
			}
		}

		public static bool Verify(byte[] data, byte[] singed, byte[] pubKey)
		{
			bool flag = false;
			RSACryptoServiceProvider rSACryptoServiceProvider = null;
			try
			{
				rSACryptoServiceProvider = new RSACryptoServiceProvider();
				rSACryptoServiceProvider.ImportCspBlob(pubKey);
				return rSACryptoServiceProvider.VerifyData(data, new SHA1CryptoServiceProvider(), singed);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				rSACryptoServiceProvider?.Clear();
			}
		}


	}
}
